CreateShootingCommandHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 28
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 22 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
4
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
5
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository';
6
import { Shooting, ShootingStatus } from 'src/Domain/School/Shooting.entity';
7
import { CreateShootingCommand } from './CreateShootingCommand';
8
9
@CommandHandler(CreateShootingCommand)
10
export class CreateShootingCommandHandler {
11
  constructor(
12
    @Inject('ISchoolRepository')
13
    private readonly schoolRepository: ISchoolRepository,
14
    @Inject('IShootingRepository')
15
    private readonly shootingRepository: IShootingRepository,
16
  ) {}
17
18
  public async execute(command: CreateShootingCommand): Promise<string> {
19
    const { name, groupClosingDate, individualClosingDate, schoolId, shootingDate, notice } = command;
20
21
    const school = await this.schoolRepository.findOneById(schoolId);
22
    if (!school) {
23
      throw new SchoolNotFoundException();
24
    }
25
26
    const shooting = await this.shootingRepository.save(
27
      new Shooting(
28
        name,
29
        shootingDate,
30
        groupClosingDate,
31
        individualClosingDate,
32
        ShootingStatus.DISABLED,
33
        school,
34
        notice
35
      )
36
    );
37
38
    return shooting.getId();
39
  }
40
}
41